package logic

import (
	"context"
	"user/moder/mysql"
	"user/pkg"
	"user/userrpc/internal/svc"
	"user/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginLogic) UserLogin(in *userrpc.UserLoginRequest) (*userrpc.UserLoginResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	user = mysql.User{
		Username: in.Username,
		Password: pkg.Md5(in.Password),
	}

	err := user.UserLogin(mysql.DB, in.Username)
	if err != nil {
		return nil, err
	}
	return &userrpc.UserLoginResponse{
		UserId: int64(user.ID),
	}, nil
}
